package breeze.pixel.weather.service.app_widget.configures;

import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

import java.io.File;

import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.utils.AppToolUtil;

public class Widget1ConfigActivity extends BaseWidgetConfigActivity {

    private View baseView;
    private ImageView bgImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public void init() {
        bgImg = baseView.findViewById(R.id.wid_get_1_layout);
    }

    @Override
    public void initData() {

    }

    @Override
    protected int getWidgetMode() {
        return WIDGET_MODE_1;
    }

    @Override
    protected void onSave() {
        try {
            int widgetID = getWidgetID();
            if (file!=null) {
                File file = new File(AppToolUtil.getTempPicFile(this));
                file.renameTo(new File(AppToolUtil.getAppWidgetBackgroundPic_1(this)));
            }
            AppWidgetManager instance = AppWidgetManager.getInstance(this);
            RemoteViews rv = new RemoteViews(getPackageName(), R.layout.widget_launcher_1);

            if (new File(AppToolUtil.getAppWidgetBackgroundPic_1(this)).exists() && new File(AppToolUtil.getAppWidgetBackgroundPic_1(this)).length() > 0) {
                Bitmap bitmap = AppToolUtil.roundedBitmap(brightness,
                        BitmapFactory.decodeFile(AppToolUtil.getAppWidgetBackgroundPic_1(this)),
                        50f);
                rv.setImageViewBitmap(R.id.wid_get_1_layout, bitmap);
            } else {
                rv.setImageViewResource(R.id.wid_get_1_layout, R.drawable.cardview_tendp_white);
            }

            rv.setInt(R.id.wid_get_1_layout, "setImageAlpha", transparency);
            rv.setTextColor(R.id.widget_clock, set_color);
            rv.setTextColor(R.id.weather_tem, set_color);
            rv.setTextColor(R.id.widget_weather_text, set_color);
            rv.setTextColor(R.id.widget_location, set_color);
            rv.setTextColor(R.id.widget_windspeed, set_color);
            rv.setTextViewText(R.id.widget_date, AppToolUtil.getSimpleDate("MM月dd日"));
            rv.setTextColor(R.id.widget_date, set_color);
            rv.setTextColor(R.id.widget_week, set_color);

            instance.updateAppWidget(widgetID, rv);
            setResult(RESULT_OK,new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,widgetID));
            finish();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void setTextViewColor(int col) {
        ((TextView) find(R.id.widget_clock)).setTextColor(col);
        ((TextView) find(R.id.weather_tem)).setTextColor(col);
        ((TextView) find(R.id.widget_weather_text)).setTextColor(col);
        ((TextView) find(R.id.widget_location)).setTextColor(col);
        ((TextView) find(R.id.widget_windspeed)).setTextColor(col);
        ((TextView) find(R.id.widget_date)).setTextColor(col);
        ((TextView) find(R.id.widget_week)).setTextColor(col);
    }


    private File file;
    private int brightness = 0;
    private int transparency = 255;
    private int set_color;

    @Override
    protected void onPictureSelected(File file) {
        this.file = file;
        bgImg.setImageBitmap(AppToolUtil.roundedBitmap(
                brightness,
                BitmapFactory.decodeFile(file.getAbsolutePath()),
                50f));
    }

    @Override
    protected void onTransparentChanged(int val) {
        this.transparency = val;
        bgImg.setImageAlpha(val);
    }

    @Override
    protected void onBrightnessChanged(int val) {
        this.brightness = val;
        bgImg.setImageBitmap(AppToolUtil.roundedBitmap(
                brightness,
                BitmapFactory.decodeFile(file.getAbsolutePath()),
                50f));
    }

    @Override
    protected void onTextColorChanged(int val) {
        this.set_color = val;
        setTextViewColor(val);
    }

    @Override
    protected View returnChildView() {
        baseView = LayoutInflater.from(this).inflate(R.layout.widget_launcher_1, null);
        return baseView;
    }


}
